package cn.com.xf.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.xf.common.AbstractBaseController;
import cn.com.xf.common.Constants;
import cn.com.xf.common.StringUtil;
import cn.com.xf.domain.TblCheckHoliday;
import cn.com.xf.domain.TblCheckMend;
import cn.com.xf.service.HolidayService;
import cn.com.xf.utils.encode.EncrypTool;
import cn.com.xf.web.PageModel;
import cn.com.xf.web.form.HolidayForm;
import cn.com.xf.web.form.LeaveForm;
import cn.com.xf.web.form.MendForm;

/**
 * 节假日管理
 *
 */
@Controller
@RequestMapping("/check")
public class HolidayController extends AbstractBaseController {

	@Autowired
	private HolidayService holidayService;
	
	@RequestMapping("/addHoliday")
	public ModelAndView addHoliday(HttpServletRequest request, HttpServletResponse response, HolidayForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_holiday", Constants.FORM_COMMAND_KEY, form);
		mv.addObject("defaultSiteid", getDefaultSiteid());
		return mv;
	}
	
	@RequestMapping("/doAddHoliday")
	public ModelAndView doAddHoliday(HttpServletRequest request, HttpServletResponse response, @Valid HolidayForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		if(!isSiteId(form.getSiteid())) {
			FieldError e = new FieldError("", "siteid", "所属公司不合法");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		// 判断结束日期是否在开始日期之后
		if (form.isEndDateBeforeStartDate()) {
			FieldError e = new FieldError("", "enddate", "结束日期不能在开始日期之前");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		// 添加
		TblCheckHoliday holiday = form.copyTo();
		holidayService.insertHoliday(holiday);
		mv.addObject("status", "SUCCESS");
		return mv;
	}

	@RequestMapping("/holidayList")
	public ModelAndView holidayList(HttpServletRequest request, HttpServletResponse response, HolidayForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/holiday_list", Constants.FORM_COMMAND_KEY, form);
		String siteid = StringUtil.StringFilter(form.getSiteid());
		if (siteid.equals("") || !isSiteId(siteid)) {
			siteid = getDefaultSiteid();
		}
		form.setSiteid(siteid);
		int count = holidayService.selectHolidayCount(siteid);
		if (count == 0)
			return mv;

		int pageNum = 1;
		if (StringUtils.isNotBlank(form.getPageNum())) {
			pageNum = Integer.valueOf(form.getPageNum());
		}
		PageModel pageModel = new PageModel(count, PageModel.DEFAULT_PAGE_SIZE, pageNum, form);
		int begin = pageModel.getBegin();
		int pageSize = pageModel.getPageSize();
		List<TblCheckHoliday> list = holidayService.selectHoliday(begin, pageSize, siteid);
		mv.addObject("list", list);
		return mv;
	}
	
	@RequestMapping("/editHoliday")
	public ModelAndView editHoliday(HttpServletRequest request, HttpServletResponse response, HolidayForm form)
			throws Exception {
		ModelAndView mv = new ModelAndView("/check/edit_holiday", Constants.FORM_COMMAND_KEY, form);
		Long id = new Long(EncrypTool.dencode(form.getId()));
		TblCheckHoliday holiday = holidayService.selectHolidayById(id);
		mv.addObject("holiday", holiday);
		return mv;
	}
	
	@RequestMapping("/doEditHoliday")
	public ModelAndView doEditHoliday(HttpServletRequest request, HttpServletResponse response, @Valid HolidayForm form,
			BindingResult result) throws Exception {
		ModelAndView mv = new ModelAndView("/check/add_result", Constants.FORM_COMMAND_KEY, form);
		List<ObjectError> errorList = null;
		if (result.hasErrors()) {
			errorList = result.getAllErrors();
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		if(!isSiteId(form.getSiteid())) {
			FieldError e = new FieldError("", "siteid", "所属公司不合法");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		// 判断结束日期是否在开始日期之后
		if (form.isEndDateBeforeStartDate()) {
			FieldError e = new FieldError("", "enddate", "结束日期不能在开始日期之前");
			errorList = new ArrayList<ObjectError>();
			errorList.add(e);
			String errorJson = errorToJson(errorList);
			mv.addObject("errorJson", errorJson);
			return mv;
		}
		String idStr = form.getId();
		String realId = "";
		if (StringUtils.isNotBlank(idStr)) {
			realId = EncrypTool.dencode(idStr);
		}
		TblCheckHoliday holiday = holidayService.selectHolidayById(new Long(realId));
		if (holiday != null) {
			holiday = form.copyTo(holiday);
			int count = holidayService.updateHoliday(holiday);
			if (count > 0) {
				mv.addObject("status", "SUCCESS");
			}
		}
		return mv;
	}

	@RequestMapping("/deleteHoliday")
	@ResponseBody
	public Map<String, Object> deleteHoliday(HttpServletRequest request, HttpServletResponse response, HolidayForm form)
			throws Exception {
		String idStr = form.getId();
		String realId = "";
		if (StringUtils.isNotBlank(idStr)) {
			realId = EncrypTool.dencode(idStr);
		}
		int result = holidayService.deleteHoliday(new Long(realId));
		Map<String, Object> map = new HashMap<String, Object>();
		if (result > 0) {
			map.put("status", "FAILD");
		} else {
			map.put("status", "SUCCESS");
		}
		return map;
	}
	
}
